package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IUserCoinDao;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.Source;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.UserCoin;
import com.magicode.cms.entiy.base.UserSubscription;
import com.magicode.cms.service.IUserCoinService;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.dao.PageResults;

@Service("serviceusercoin")
public class UserCoinServiceImpl extends BaseServiceImpl<UserCoin,IUserCoinDao> implements IUserCoinService,UtilPath{
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			return this.queryMapDataList(hql, gridsel, 0, 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	
	
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		return beaninfo;
	}
	@Autowired
	@Qualifier("daoUserCoin")
	private IUserCoinDao UserCoinDao;
	public IUserCoinDao getDao() throws Exception {
		return UserCoinDao;
	}
	public IUserCoinDao getTagDao() {
		return UserCoinDao;
	}

	public void setTagDao(IUserCoinDao usercoinDao) {
		this.UserCoinDao = usercoinDao;
	}
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.UserCoin.class.getName()+" "+entity_alias_name+" where 1=1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey("user")){
				hql = hql+" and  "+entity_alias_name+".user.id='"+map.get("user")+"'";
			}
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
		}
		return hql;
	}



	@Override
	public UserCoin getUserCoin(String userid)throws Exception{
		UserCoin uc = null;
		if(null!=userid && !"".equals(userid)) {
			Map<String,String> map = new HashMap<String,String>();
			map.put("user", userid);
			String hql = this.getHql(map);
			List<UserCoin> list = UserCoinDao.queryDataList(hql, 0, 0);
			if(list.size()==0){
				uc = new UserCoin();
				User user = (User) UserCoinDao.getObject(User.class.getName(), userid);
				uc.setUser(user);
				uc.setCreateTime(new Date());
				uc.setCoinCount(0);
				UserCoinDao.createObject(uc);
				System.out.println(uc.getId());
			}else{
				uc = list.get(0);
			}
		}
		return uc;
	}
	
	@Override
	public PageResults getSubscriptionNewsByUser(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		
		String hql = " select distinct n from "+UserSubscription.class.getName()+" t , "+News.class.getName()+" n  left outer join fetch n.mainPic  left outer join n.source ";
		String hql_where =" where t.tag in elements(n.tags) ";
//		按照频道设置排序
		String hql_order =" order by n.createTime desc, n.clickCount desc ";
		if(null!=prams && !prams.isEmpty()){
			//某标签 id 的文章
			if(prams.containsKey("userid")){
					hql_where+=" and t.user.id='"+prams.get("userid")+"' ";
			}
			
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		if(null != pageResults && pageResults.getResults().size() > 0){
			String hql1 = "select distinct new map(t.id as id , d.title as title ) from "+Source.class.getName()+" d , "+News.class.getName()+" t "
					+ " where  d=t.source " ;
			String propertyName = "sourceName";
			try {
				pageResults = this.getRelateProByList(hql1, ",", propertyName, pageResults);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pageResults;
	}
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}

	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}
}
